package com.sgb.goods.view.fragment;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;

import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsFragmentShopPriceBinding;
import com.sgb.goods.entity.ShopMyPriceListEntity;
import com.sgb.goods.utils.base.fragment.BaseFragment;
import com.sgb.goods.utils.helper.SmartRefreshLayoutHelper;
import com.sgb.goods.viewmodel.ShopMyPriceModel;
import com.sgb.goods.viewmodel.ShopMyPriceViewModel;

import java.util.Objects;

/**
 * 我的报价列表界面
 * @author cyj
 */
public class ShopMyPriceFragment extends BaseFragment<GoodsFragmentShopPriceBinding, ShopMyPriceModel>
        implements SmartRefreshLayoutHelper {

    private int dataType = 1;
    private String searchContent = "";
    private ShopMyPriceViewModel mViewModel2;

    public ShopMyPriceFragment(int type) {
        this.dataType = type;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_fragment_shop_price;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(ShopMyPriceModel.class, BR.viewModel);
        mViewModel2 = createParentViewModel(ShopMyPriceViewModel.class);
    }

    @Override
    public void lazyLoadData() {
        // 获取订单列表
        mViewModel.getDataList(dataType, searchContent, false);
    }

    @Override
    protected void initObserve() {
        // 解决 activity 软件盘收起时搜索内容未更新问题
        mViewModel2.bindSearchContent.observe(this, bindSearchContent -> {
            if(!bindSearchContent.equals(mViewModel2.searchContent.getValue())) {
                this.searchContent = bindSearchContent;
            }
        });
        // activity 页面内容变更回调通知与处理
        mViewModel2.searchContent.observe(this, searchContent -> {
            // 为所有显示与未显示的 fragment 页面进行搜索内容变量覆盖
            this.searchContent = searchContent;
            // 当前 fragment 显示，则调用网络请求带搜索内容的数据
            if(Lifecycle.Event.ON_RESUME == currentLifecycleEvent) {
                mViewModel.getDataList(dataType, searchContent, false);
            }
        });
        mViewModel.dataList.observe(this, dataList -> {
            refreshUi(mDataBinding.rlvLayout.refreshLayout, mViewModel.adapter, dataList);
        });
    }

    @Override
    protected void initListener() {
        mDataBinding.rlvLayout.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mViewModel.getDataList(dataType, searchContent, false);
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mViewModel.getDataList(dataType, searchContent, true);
            }
        });
        // 点击跳转详情
        mViewModel.adapter.setOnItemClickListener((adapter, view, position) -> {
            mViewModel.getDataInfo(((ShopMyPriceListEntity) Objects.requireNonNull(adapter.getItem(position))).no);
        });
    }
}